package com.android.ddfafu.domian;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

import com.android.ddfafu.baiduLocation.LocationUtil;
import com.android.ddfafu.result.MyBundleData;
import com.android.ddfafu.utils.BitmapUtil;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @description 在应用运行期间 用来存储Bundle数据
 */
public class StaticBundleData {

    private String usernameSub;
    private SharedPreferences sp_img;
    private SharedPreferences sp_user;
    private List<Bundle> bundleList;
    private LatLng latLng;

    public StaticBundleData(Context context) {
        bundleList = new ArrayList<>();

        //将 本地存储的照片路径转成 百度地图的 MarkerOptions 存进Application中
      /*  sp_img = context.getSharedPreferences("userImages", Context.MODE_WORLD_WRITEABLE);
        sp_user = context.getSharedPreferences("userData", Context.MODE_WORLD_WRITEABLE);
        Set<String> image_path_set = sp_img.getStringSet("image_path", null);
        List<String> image_path_list = new ArrayList<>(image_path_set);
        String usernameSub = sp_user.getString("USERNAME", "").replace("@", "").replace(".", "");
        if (image_path_list.size() != bundleList.size()) {
            bundleList.clear();
            Iterator<String> iterator = image_path_set.iterator();
            int index = 0;
            while (index < image_path_set.size() - bundleList.size()) {
                String path = iterator.next();
                //path.substring(27,path.indexOf("com"));
                if (path.contains(usernameSub)) {
                    File file = new File(path);
                    Uri uri = Uri.fromFile(file);
                    Bitmap bitmap = BitmapUtil.getBitmapFromUri(context, uri);
//                    Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
                    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
                    OverlayOptions option = new MarkerOptions()
                            .position() //必传参数
                            .icon(bitmapDescriptor)
                            .scaleX(0.05f)
                            .scaleY(0.05f)
                            .perspective(true); //必传参数
                    Bundle bundle = new Bundle();
                    MyBundleData myBundleData = new MyBundleData(option);
                    bundle.putSerializable("MARKER", myBundleData);
                    bundleList.add(bundle);
                }
            }

        }*/
       /* new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(250);
                    latLng = LocationUtil.getLatLng();
                    if (latLng != null) {
                        sp_img = context.getSharedPreferences("userImages", Context.MODE_PRIVATE);
                        sp_user = context.getSharedPreferences("userData", Context.MODE_PRIVATE);
                        usernameSub = sp_user.getString("USERNAME", "").replace("@", "").replace(".", "");
                        Set<String> user_img_path_set = sp_img.getStringSet(usernameSub, null);
                        if (user_img_path_set != null) {
                            bundleList.clear();
                            for (String path : user_img_path_set) {
                                File file = new File(path);
                                if (file.exists()) {
                                    Uri uri = Uri.fromFile(file);
                                    Bitmap bitmap = BitmapUtil.getBitmapFromUri(context, uri);
                                    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
                                    OverlayOptions option = new MarkerOptions()
                                            .position(latLng) //必传参数
                                            .icon(bitmapDescriptor)
                                            .scaleX(0.05f)
                                            .scaleY(0.05f)
                                            .perspective(true); //必传参数
                                    Bundle bundle = new Bundle();
                                    MyBundleData myBundleData = new MyBundleData(option);
                                    bundle.putSerializable("MARKER", myBundleData);
                                    addBundle(bundle);
                                }
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();*/

    }
    public List<Bundle> getBundleList() {
        return bundleList;
    }

    public void addBundle(Bundle bundle) {
        this.bundleList.add(bundle);
    }

}
